package com.fuershiye.woman.monitor.controller;

import com.fuershiye.woman.monitor.beans.*;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.model.FormsDepart;
import com.fuershiye.woman.monitor.model.Indicator;
import com.fuershiye.woman.monitor.service.*;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;
import com.fuershiye.woman.monitor.utils.MyUtilString;
import com.fuershiye.woman.monitor.utils.PageUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 按年份进行报表的查询
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/report/sex")
@SuppressWarnings("unchecked")
public class ReportSexController {

	@Autowired
	ReportService reportService;
	@Autowired
	IndicatorService indicatorService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	FormsDepartService formsDepartService;
	@Autowired
	FormsService formsService;

	@RequestMapping(value = "/sexView.do")
	public String preAlarm(String show, Model model) {

		List<Integer> years = DateFormatUtil.getAreaYears();
		model.addAttribute("years", years);

		List<IdName> formes = formsService.getAllNames();
		// 表 forms 的ID ： 1:妇儿 2:社会
		model.addAttribute("formes", formes);

		return "sex/" + show;
	}

	/**
	 * 接口文档【6.1、同部门,多年份,模糊指标】
	 * 	http://bug.fuershiye.com/doc-view-25.html
	 *
	 * 同部门,多年份,模糊指标
	 *
	 * @param departid	【必须，-1:全部，只有省级部门的】
	 * @param iname	【非必须】
	 * @param years 【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param page	【非必须】
	 * @param pageSize【非必须】
	 *
	 * @return
	 */
	@RequestMapping(value = "/depart.do")
	@ResponseBody
	// todo aimYear? 干啥的，直接给跳过了
	public Result depart(String departid, Long formsid, String iname, String years, Integer aimYear, Integer page, Integer pageSize) {

		if (StringUtils.isBlank(years) || departid == null || StringUtils.isBlank(departid)
				|| formsid == null || formsid==0) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if (yearList.isEmpty()) {
			return new Result(Massage.ERROR_YEAR);
		}

		List<Long> departidTemp;
		if(departid.equals("-1")){
			departidTemp = null;
		}else{
			departidTemp= MyUtilString.str2LongByPlaceHolder(departid, ",");
		}

		// 妇儿报表
		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOpposite(departidTemp, yearList, formsid, 0, new Page(1, Integer.MAX_VALUE));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		//只查询存在上报关系年份的数据
		yearList = formsDepartService.getYears(formsDepartList);

		List<Long> departIds = Lists.newArrayList();
		// 全部，默认只查找所有省级的部门
		if(departid.equals("-1")){
			List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(2, null, 1);
			departIds = departmentService.getDepartIdsFromIdNameSname(departmentList);
		}else{
			departIds= MyUtilString.str2LongByPlaceHolder(departid, ",");
		}
		if (departIds.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		// 未删除状态数据
		int status = 1;

		List<Indicator> indicatorList = indicatorService.listByCondition(1, formsid, status);
		List<Long> indicators = indicatorService.getIndicatorId(indicatorList);
		if(indicators==null||indicators.size()<1){
			List<Map<String, Object>> result = Lists.newArrayList();
			return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), 0));
		}

		List<ViewReportAllValue> reportList = reportService.getReportDate(departIds, formsid, indicators, yearList, status);
		List<Map<String, Object>> result = reportService.filter(reportList, yearList);

		return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));

	}

	/**
	 * 接口文档【6.3、同部门,多年份,模糊指标】
	 * 	http://bug.fuershiye.com/doc-view-27.html
	 *
	 * 同地域,同领域,多年份,模糊查询
	 *
	 * @param areaid	【必传,地域】
	 * @param iname		【非必传,模糊指标名称查询】
	 * @param years 【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param page	【非必传】
	 * @param pageSize	【非必传】
	 * @return	返回值是value_color
	 *
	 */
	//	@RequiresPermissions("report:preAlarm")
	@RequestMapping(value = "/area.do")
	@ResponseBody
	// todo aimYear? 干啥的，直接给跳过了
	public Result area(String departid, Long areaid, String iname, Long formsid, String years, Integer aimYear, Integer page, Integer pageSize) {

		if(areaid==null||areaid == 0 || formsid == null || formsid==0 || StringUtils.isBlank(years)) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if(yearList.isEmpty()){
			return new Result(Massage.ERROR_YEAR);
		}


		List<Long> departids = Lists.newArrayList();
		if(departid.equals("-1")){
			departids = null;
		}else{
			departids= MyUtilString.str2LongByPlaceHolder(departid, ",");
		}
		//有了部门id，则直接用部门ID，没有按地域查询
		if(departids==null || departids.size()<1){
			List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(null, areaid, 1);
			departids=departmentService.getDepartIdsFromIdNameSname(departmentList);
		}

		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOpposite(departids, yearList, formsid, 0, new Page(1, Integer.MAX_VALUE));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}

		// 查找所有的指标
		List<Indicator> indicatorAll = indicatorService.listByCondition(iname, formsid, null, null, 1,1, new Page(1, Integer.MAX_VALUE));
		List<Long> indicatorIds = indicatorService.getIndicatorId(indicatorAll);
		if (indicatorIds.isEmpty()) {
			return new Result(Massage.INDICATOR_IS_NULL);
		}

		List<ViewReportAllValue> reportList = reportService.getReportDate(departids, formsid, indicatorIds, yearList, 1);
		List<Map<String, Object>> result = reportService.filter(reportList, yearList);

		return new Result(Massage.SUCCESS, new PageResult(PageUtil.getPage(new Page(page, pageSize), result), result.size()));

	}

}
